package com.jy.datapipeline.export.quartz;

import com.jy.datapipeline.common.util.BeanFactoryUtil;
import com.jy.datapipeline.export.entity.ExpTaskExecuteLogModel;
import com.jy.datapipeline.export.entity.ExpTaskModel;
import com.jy.datapipeline.export.service.ExpTaskExecuteDetailLogService;
import com.jy.datapipeline.export.service.ExpTaskExecuteLogService;
import com.jy.datapipeline.export.service.ExpTaskService;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class JobDetailTaskStatusBySuccessHandler extends QuartzJobBean {
//    @Autowired
//    private  ExpTaskService expTaskService;
//    @Autowired
//    private  ExpTaskExecuteLogService expTaskExecuteLogService;
//    @Autowired
//    private  ExpTaskExecuteDetailLogService expTaskExecuteDetailLogService;
//    public JobDetailTaskStatusBySuccessHandler(ExpTaskService expTaskService,
//        ExpTaskExecuteLogService expTaskExecuteLogService,
//        ExpTaskExecuteDetailLogService expTaskExecuteDetailLogService) {
//        this.expTaskService = expTaskService;
//        this.expTaskExecuteLogService = expTaskExecuteLogService;
//        this.expTaskExecuteDetailLogService = expTaskExecuteDetailLogService;
//    }


    @Override
    protected void executeInternal(JobExecutionContext context)  {
        ExpTaskService expTaskService = BeanFactoryUtil.getBean(ExpTaskService.class);
        ExpTaskExecuteLogService expTaskExecuteLogService = BeanFactoryUtil.getBean(ExpTaskExecuteLogService.class);
        ExpTaskExecuteDetailLogService expTaskExecuteDetailLogService = BeanFactoryUtil.getBean(ExpTaskExecuteDetailLogService.class);
        try {
            List<ExpTaskExecuteLogModel> expTaskExecuteLogModelList= expTaskExecuteLogService.findByStatus(4);
            if(expTaskExecuteLogModelList.size()>0){
                AtomicInteger successCount= new AtomicInteger();
                expTaskExecuteLogModelList.forEach( executeLogModelList ->{
                    List<Map<String,Object>> list=    expTaskExecuteDetailLogService.findBytaskExecutelogLogIds(executeLogModelList.getTaskExecuteLogId());
                    // List<ExpTaskExecuteDetailLogModel> list=  this.expTaskExecuteDetailLogService.findBytaskExecutelogLogId(executeLogModelList.getTaskExecuteLogId());
                    if(list.size()>0){
                        int exp_status=Integer.parseInt( list.get(0).get("exp_status")==null?"0":list.get(0).get("exp_status").toString());
                        //list.forEach( detaillist->{
                        if(exp_status==7){
                            successCount.getAndIncrement();
                        }
                        if(successCount.get()==list.size()){
                            expTaskExecuteLogService.updateExeuceStatus(executeLogModelList.getTaskExecuteLogId(),2,0,"","");
                            ExpTaskModel taskModel= expTaskService.findByTaskId(executeLogModelList.getTaskId());
                            if(taskModel!=null){
                                if(taskModel.getTaskType()==1){
                                    expTaskService.updateExpTaskStatus(2,executeLogModelList.getTaskId(),"");
                                }
                            }
                        }
                    }
                });
            }
        }catch (Exception e){
        log.error("JobDetailTaskStatusBySuccessHandler error",e);
        }

    }
}
